package com.shanyue.redwine.fragment;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import cn.pedant.SweetAlert.SweetAlertDialog;
import com.shanyue.redwine.R;
import com.shanyue.redwine.activity.ActivityBase;
import com.shanyue.redwine.entity.Page;
import com.shanyue.redwine.http.RestClient;
import com.shanyue.redwine.utils.Utils;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * 项目：MeiYueHongJiu  包名：com.shanyue.app.locationdemo.fragment
 * <p>
 * huangxiaoyu
 * <p>
 * 2018/5/7
 * <p>
 * desc
 */
public abstract class FragmentBase<T extends ViewDataBinding> extends Fragment {
    RestClient mRestClient;
    SweetAlertDialog mLoadingDialog;
    protected ActivityBase mBaseActivity;//避免系统回收activity.生命一个全局的mContext
    public String TAG;
    CompositeSubscription mCompositeSubscription;
    T mBinding;
    Page mPage;

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            (getActivity()).setTitle(getTitle());
        }
    }


    /**
     * 布局id
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * activity标题
     *
     * @return
     */
    protected abstract String getTitle();


    /**
     * 初始化页面
     *
     * @param saveInstanceState
     */
    protected abstract void initView(Bundle saveInstanceState);


    protected ActivityBase getBaseActivity() {
        return mBaseActivity;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        TAG = this.getClass().getSimpleName();
        getActivity().setTitle(getTitle());
        mBaseActivity = (ActivityBase) getActivity();
        mRestClient = new RestClient(getBaseActivity());
        mLoadingDialog = new SweetAlertDialog(getBaseActivity(), SweetAlertDialog.PROGRESS_TYPE).setTitleText(getString(R.string.LOADING));
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
        initView(savedInstanceState);
        return mBinding.getRoot();
    }

    public void addSubscription(Subscription s) {
        if (this.mCompositeSubscription == null) {
            this.mCompositeSubscription = new CompositeSubscription();
        }
        this.mCompositeSubscription.add(s);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (this.mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            this.mCompositeSubscription.unsubscribe();
        }
    }

    public void removeSubscription() {
        if (this.mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            this.mCompositeSubscription.unsubscribe();
        }
    }

    /**
     * 弹出toast
     *
     * @param msg
     */
    void showToast(String msg) {
        Utils.showToast(getActivity(), msg);
    }

    //    @Override
    //    public void onResume() {
    //        super.onResume();
    //        MobclickAgent.onPageStart(this.getClass().getSimpleName()); //统计页面("MainScreen"为页面名称，可自定义)
    //    }
    //
    //    @Override
    //    public void onPause() {
    //        super.onPause();
    //        MobclickAgent.onPageEnd(this.getClass().getSimpleName());//统计页面
    //    }
}
